import{
  getBidCart,
  myBidProducts
}from "../../../api.js"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iphoneX:false,
    shoplist:[],
    none:true,
    outlist:true,
    color:false,
    weixin:"Brandx11",
    chuopai:true,
    carList:[],
    total:0,
    allCheck:false,
    btn:false,
    outpage:1,
    outlastpage:2
  },
  //选择出品
  choose(){
    wx.redirectTo({
      url: 'choose/choose',
    })
  },
  // 极速出品
  outshop(){
    wx.redirectTo({
      url: 'outshop/outshop',
    })
  },
  shoplist(){
    this.setData({
      outlist: false
    })
  },
  changetrue(){
    if(this.data.color){
      return
    }
    this.data.shoplist.length>0?this.setData({
      none:true,
      color: true,
      carList:[]
    }):this.setData({
      color: true
    })
    this.myproduct(1)
  },
  changefalse() {
    if (!this.data.color) {
      return
    }
    this.data.carList.length>0?this.setData({
      none:true,
      color: false,
      shoplist:[]
    }) : this.setData({
      color: false
    })
   this.product(1);
  },
  myproduct(page){
    this.setData({
      color:true
    })
    var token = app.globalData.token
    myBidProducts({
      token,
      page
    }).then(res=>{
      if(res.data.code===666){
        console.log(res.data.data.current_page)
        if (res.data.data.data.length>0){
          res.data.data.data.forEach(r=>{
            if (r.status===0){
              r.alt = "待审核"
            } else if (r.status===1){
              r.alt ="审核通过"
            } else if (r.status === 2){
              r.alt = "审核不通过"
            }
          })
          
          if(res.data.data.current_page==1){
            if(res.data.data.data.length==0){
              this.setData({
                none: false,
                btn: true
              })
            }else{
              this.setData({
                none: true,
                shoplist: res.data.data.data,
                outpage: res.data.data.current_page,
                outlastpage: res.data.data.last_page,
                btn: false
              })
            }
           
          }else{
            this.setData({
              none: true,
              shoplist: this.data.shoplist.concat(res.data.data.data),
              outpage: res.data.data.current_page,
              outlastpage: res.data.data.last_page,
              btn: false
            })
          }   
        }else{
          if (res.data.data.current_page == 1){
            this.setData({
              none: false,
              btn: true
            })
          }else{
            
          }
        
        }  
      }else{
        wx.showToast({
          title: res.data.msg,
          icon:"none"
        })
      }
    })
    console.log(this.data.shoplist)
  },
  // 获取获拍商品
  product(page){
    this.setData({
      btn: true,
      carList:[]
    })
    getBidCart({ token: app.globalData.token,
    page}).then(res => {
      if(res.data.data.current_page==1){
          if(res.data.data.lenght>0){
            this.setData({
          carList: res.data.data,
          none: true,
          btn:false
        })
          }else{
            this.setData({
          none: false,
          btn:true
        })
          }
       
      }else{
        if (res.data.data.length>0){
          this.setData({
          carList: this.data.carList.concat(res.data.data),
          // none: true,
          // btn:false
        }) 
        }else{
          this.setData({
            none: false,
            btn:true
          })
        }
       
      }
    
      
    })
    console.log(this.data.carList)
  },
  open(){
    this.setData({
      chuopai:false
    })
  },
  close() {
    this.setData({
      chuopai: true,
      outlist:true
    })
  },
  //复制
  copy() {
    var that = this
    wx.setClipboardData({
      data: this.data.weixin,
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log(res.data) // data
            that.setData({
              chuopai: true
            })
          }
        })
      }
    })
  },
  onHide(){
    console.log("隐藏")
    this.setData({
      outlist: true,
      total: 0
    })
  },
  service() {
    wx.navigateTo({
      url: '../setUp/setUp',
    })
  },
  onShow(){
    this.data.color === false ? this.product(1) : this.myproduct(1)

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      iphoneX: app.globalData.is_iPhoneX
    })
    var that = this
    options.color && options.color ===false ? that.product(1) : this.myproduct(1)
  },
  handleCheckAll(e){
    let carList = this.data.carList;
    let total = 0;
    for(let i=0;i<carList.length;i++){
      if (carList[i].product_state < 5) {
        carList[i].isPicked = this.data.allCheck ? false : true
        total += this.data.allCheck ? 0 : carList[i].nest_price
      }
    }
    this.setData({
      carList,
      total,
      allCheck: !this.data.allCheck
    })
  },
  onReachBottom(){
    var that =this
    var page = this.data.outpage+1
    if (this.data.outpage > this.data.outlastpage){
        return
    }
    console.log(this.data.color)
    this.data.color === false ? that.product(page) : that.myproduct(page)
  },
  /**
 * 选中商品
 */
  handleProductCheck(e) {
    let index = e.currentTarget.dataset.index
    let current = this.data.carList[index]
    let total = this.data.total
    let key = `carList[${index}]`

    if (current.isPicked) {
      total -= current.nest_price
    } else {
      total += current.nest_price
    }
    /**
     * 浮点数 js 精度问题 转化为二进制计算导致的
     */
    total = parseFloat(total.toFixed(2))
    current.isPicked = !current.isPicked
    this.setData({
      [key]: current,
      total
    })
  },
  /**
 * 处理小程序options encode编码 bug
 */
  encodeJson(p) {
    let keyArr = Object.keys(p)
    let map = Object.create(null)
    let k;
    for (let i = 0, l = keyArr.length; i < l; i++) {
      k = keyArr[i]
      map[k] = encodeURIComponent(p[k])
    }
    return map
  },
  createOrder(){
    let pickedArr = this.data.carList.reduce((a, b) => {
      if (b.isPicked) {
        a.push(this.encodeJson(b))
      }
      return a
    }, [])
    if (pickedArr.length) {
      wx.navigateTo({
        url: '/pages/goodControl/toBuy/confirmBuy/confirmBuy?product=' + JSON.stringify(pickedArr)
      })
    } else {
      wx.showToast({
        title: '抱歉，您还没有挑选需要结算的商品',
        icon: "none"
      })
    }
  }
})